package utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesUtil {

    // 配置文件路径
    private static final String CONFIG_FILE = "src\\properties\\mysql-connection.properties";

    // 加载配置文件的方法
    private static Properties loadProperties() throws IOException {
        Properties properties = new Properties();
        try (FileInputStream fis = new FileInputStream(CONFIG_FILE)) {
            properties.load(fis);
        }
        return properties;
    }

    // 获取driver属性
    public static String getDriver() {
        try {
            return loadProperties().getProperty("driver");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取url属性
    public static String getUrl() {
        try {
            return loadProperties().getProperty("url");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取username属性
    public static String getUsername() {
        try {
            return loadProperties().getProperty("username");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取password属性
    public static String getPassword() {
        try {
            return loadProperties().getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
